package com.seveneight.baijiesteward.ui.main.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.base.MessageEvent;
import com.seveneight.baijiesteward.ui.main.contract.ForgetPwdContract;
import com.seveneight.baijiesteward.ui.main.presenter.ForgetPwdPresenter;
import com.seveneight.baijiesteward.utils.BusUitls;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.NumberUtil;
import com.seveneight.baijiesteward.utils.PreferencesUtil;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.utils.TimeCountUtil;
import com.seveneight.baijiesteward.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * Created by user1 on 2018/2/11.
 */

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener, ForgetPwdContract.ForgetPwdView {

    ForgetPwdContract.ForgetPwdPresenter forgetPwdPresenter;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_conde)
    EditText et_conde;
    @BindView(R.id.tv_sendcode)
    TextView tv_sendcode;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.et_pwd_s)
    EditText et_pwds;
    @BindView(R.id.cb_view)
    CheckBox cb_view;
    @BindView(R.id.cb_view_s)
    CheckBox cb_views;
    @BindView(R.id.bt_submit)
    Button bt_submit;
    private int type;
    private static int TYPE_CHANGEDEALPWD = 1;
    private static int TYPE_CHANGELOGINPWD = 2;
    private String phone;
    private TimeCountUtil timeCountUtil;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_forgetpwd);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getInt("type");
            tv_title.setText(type == TYPE_CHANGEDEALPWD ? "设置交易密码" : "修改登录密码");
        }
    }

    @Override
    protected void initData() {
        if (setting.isLogin()) {
            et_phone.setText(setting.getUserInfo().getMobilePhone());
            et_phone.setEnabled(false);
        }
        forgetPwdPresenter = new ForgetPwdPresenter(this);
        startTimer();
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_sendcode.setOnClickListener(this);
        cb_view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
                if (flag) {
                    et_pwd.setTransformationMethod(null);
                    et_pwd.setSelection(et_pwd.getText().toString().length());
                } else {
                    et_pwd.setTransformationMethod(new PasswordTransformationMethod());
                    et_pwd.setSelection(et_pwd.getText().toString().length());
                }
            }
        });
        cb_views.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
                if (flag) {
                    et_pwds.setTransformationMethod(null);
                    et_pwds.setSelection(et_pwds.getText().toString().length());
                } else {
                    et_pwds.setTransformationMethod(new PasswordTransformationMethod());
                    et_pwds.setSelection(et_pwds.getText().toString().length());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_sendcode:
                getCode();
                break;
            case R.id.bt_submit:
                submitChange();
                break;
        }
    }

    private void getCode() {
        phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showDialog("请输入手机号");
            return;
        }

        if (!NumberUtil.isPhoneNumber(phone)) {
            showDialog("请输入正确的手机号");
            return;
        }
        String codeType = (type == TYPE_CHANGEDEALPWD ? "11" : "10");
        forgetPwdPresenter.getCode(activity, jsonFormat.getAuthCode(phone, codeType));
//        forgetPwdPresenter.getCode(phone, codeType, "123");
    }

    private void submitChange() {
        phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showDialog("请输入手机号");
            return;
        }

        if (!NumberUtil.isPhoneNumber(phone)) {
            showDialog("请输入正确的手机号");
            return;
        }
        String editCode = et_conde.getText().toString().trim();
        if (TextUtils.isEmpty(editCode)) {
            showDialog("请输入验证码");
            return;
        }
        String password = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            showDialog("请输入密码");
            return;
        }

        if (password.length() < 6) {
            showDialog("请输入6位以上的密码");
            return;
        }
        if (!password.equals(et_pwds.getText().toString().trim())){
            showDialog("两次密码不一致");
            return;
        }
        if (type == TYPE_CHANGEDEALPWD) {
            forgetPwdPresenter.changeTradePwd(activity, jsonFormat.updateTracePwd(editCode, password));
        } else {
            forgetPwdPresenter.changeLoginPwd(activity, jsonFormat.updateLoginPwd(phone,editCode, password));
        }
    }

    @Override
    public void rebackCode() {
        StringUtils.timeCode=System.currentTimeMillis();
        startTimer();
    }

    @Override
    public void rebackLoginPwd() {
        showFinishDialog("修改密码成功!");
    }

    @Override
    public void rebackTradePwd() {
        showFinishDialog("修改密码成功!");
        try {
            userBean.setIsSetTradePassword(true);
            PreferencesUtil.put(getContext(),PreferencesUtil.userinfo, JSON.toJSONString(userBean));
            MessageEvent event=new MessageEvent(110);
            event.setMessage(BusUitls.LOGINSUCCESS);
            EventBus.getDefault().post(event);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void startTimer() {
        if ((System.currentTimeMillis()-StringUtils.timeCode)<=60000){
            timeCountUtil = new TimeCountUtil(this, (60-((System.currentTimeMillis()-StringUtils.timeCode)/1000))*1000, 1000, tv_sendcode);
            timeCountUtil.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCountUtil != null) {
            timeCountUtil.cancel();
        }
    }
}
